package com.starter.home.controller;

import com.starter.home.domain.vo.FileVo;
import com.starter.home.service.IServeImgService;
import com.starter.home.util.MD5Util;
import com.starter.home.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author jian.li
 * @date 2020年 05月01日 15:59:37
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileContrller {
    @Autowired
    private IServeImgService serveImgService;

    @Value("${file.fileUploadPath}")
    private String fileUploadPath;

    @GetMapping(value = "/delete/{fileName}")
    public ResponseResult deleteImg(@PathVariable String fileName){
        int i = serveImgService.deleteByName(fileName);
        if ( i > 0){
            return ResponseResult.success(null);
        }else{
            return ResponseResult.error(null);
        }
    }

    @GetMapping(value = "/info/{id}")
    public ResponseResult getImg(@PathVariable Integer id){
        List<FileVo> list = serveImgService.selectById(id);
        return ResponseResult.success(list);
    }

    @PostMapping(value = "/upload")
    public ResponseResult imgUpload(HttpServletRequest req, MultipartHttpServletRequest multiReq)
            throws IOException {
        String id = req.getParameter("id");
        //我这里用的springboot 在application.properties中配置，使用@Value 获取的文件上传目录
        log.info("---" + fileUploadPath);
        MultipartFile file = multiReq.getFile("file");
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.indexOf("."));
        String localFileName = MD5Util.md5(file.getInputStream()) + suffix;
        File localFile = new File(fileUploadPath + localFileName);
        if (!localFile.exists()) {
            localFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(
                    localFile);
            List<MultipartFile> file1 = multiReq.getMultiFileMap().get("file");
            FileInputStream fs = null;
            for (MultipartFile multipartFile : file1) {
                fs = (FileInputStream) multipartFile.getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fs.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
            }
            fos.close();
            fs.close();
        } else {
            log.info("文件已存在！！");
        }
        serveImgService.saveImg(Integer.parseInt(id),localFileName,"http://localhost:8080/img/"+localFileName);
        return ResponseResult.success("http://localhost:8080/img/" + localFileName);
    }

}
